package me.xuym.crawler.house58;

import com.cbs.java.component.application.Application;
import me.xuym.crawler.Processor;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by broche on 05/08/2017.
 */
public class House58Convertor extends Processor<String, List<House58Result>> {
    public House58Convertor(Application application) {
        super(application);
    }

    @Override
    protected List<House58Result> onProcess(String input) throws Exception {
        String contentStr = input.replace("\n", "");
        Pattern itemPattern = Pattern.compile("<li logr=\"[^\"]*\" _pos[^>]*>.*?</li>");
        Pattern info1Pattern = Pattern.compile("<a href=\"([^\"]*)\"[^>]*>\\s*<img data-src=\"([^\"]*)\"");
        Pattern info2Pattern = Pattern.compile("<h2[^>]*>\\s*<a[^>]*>(.*?)</a>");
        Pattern info3Pattern = Pattern.compile("<span>(.*?)</span>\\s*<span>(.*?)</span>\\s*<span>(.*?)</span>\\s*<span>(.*?)</span>");
        Pattern info4Pattern = Pattern.compile("<span>(.*?)</span>");
        Pattern info5Pattern = Pattern.compile("<div class='jjrinfo'>(.*?)－[^<]*?<.*?<span class='jjrname-outer'>(.*?)</span>");
        Pattern info6Pattern = Pattern.compile("<p class='unit'>(.*?)</p>");
        Pattern info7Pattern = Pattern.compile("<div class=\"time\">(.*?)</div>");
        Matcher matcher = itemPattern.matcher(contentStr);

        List<House58Result> results = new ArrayList<>();
        while (matcher.find()) {
            House58Result result = new House58Result();
            String itemContent = matcher.group(0);

            Matcher matcher1 = info1Pattern.matcher(itemContent);
            if (matcher1.find()) {
                result.setUrl(cleanString(matcher1.group(1)));
                result.setImage(cleanString(matcher1.group(2)));
            } else {
                continue;
            }
            itemContent = itemContent.substring(itemContent.indexOf(matcher1.group(0)) + matcher1.group(0).length());

            Matcher matcher2 = info2Pattern.matcher(itemContent);
            if (matcher2.find()) {
                result.setTitle(cleanString(matcher2.group(1)));
            } else {
                continue;
            }
            itemContent = itemContent.substring(itemContent.indexOf(matcher2.group(0)) + matcher2.group(0).length());

            Matcher matcher3 = info3Pattern.matcher(itemContent);
            if (matcher3.find()) {
                result.setDescription(String.format("%s, %s, %s, %s", cleanString(matcher3.group(1)), cleanString(matcher3.group(2)), cleanString(matcher3.group(3)), cleanString(matcher3.group(4))));

                double size = 0;
                Pattern sizePattern = Pattern.compile("[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|[1-9]\\d*");
                Matcher sizeMatcher = sizePattern.matcher(matcher3.group(2));
                if (sizeMatcher.find()) {
                    size = Double.parseDouble(sizeMatcher.group());
                }
                result.setSize(size);
            } else {
                continue;
            }
            itemContent = itemContent.substring(itemContent.indexOf(matcher3.group(0)) + matcher3.group(0).length());

            Matcher matcher4 = info4Pattern.matcher(itemContent);
            if (matcher4.find()) {
                result.setAddressdetail(cleanString(matcher4.group(1)));
                String[] addresses = result.getAddressdetail().split("－");
                result.setAddress(addresses[0]);
            } else {
                continue;
            }
            itemContent = itemContent.substring(itemContent.indexOf(matcher4.group(0)) + matcher4.group(0).length());

            Matcher matcher5 = info5Pattern.matcher(itemContent);
            if (matcher5.find()) {
                result.setPublisher(String.format("%s, %s", cleanString(matcher5.group(1)), cleanString(matcher5.group(2))));
            } else {
                continue;
            }
            itemContent = itemContent.substring(itemContent.indexOf(matcher5.group(0)) + matcher5.group(0).length());

            Matcher matcher6 = info6Pattern.matcher(itemContent);
            if (matcher6.find()) {
                double unitPrice = 0;
                Pattern pricePattern = Pattern.compile("[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|[1-9]\\d*");
                Matcher priceMatcher = pricePattern.matcher(cleanString(matcher6.group(1)));
                if (priceMatcher.find()) {
                    unitPrice = Double.parseDouble(priceMatcher.group());
                }
                result.setUnitprice(unitPrice);
                result.setTotalprice(unitPrice * result.getSize());
            } else {
                continue;
            }
            itemContent = itemContent.substring(itemContent.indexOf(matcher6.group(0)) + matcher6.group(0).length());

            Matcher matcher7 = info7Pattern.matcher(itemContent);
            if (matcher7.find()) {
                result.setDatestamp(cleanString(matcher7.group(1)));
            } else {
                continue;
            }

            results.add(result);
        }
        return results;
    }

    private String cleanString(String string) {
        Pattern pattern = Pattern.compile("<[^>]*>");
        Matcher matcher = pattern.matcher(string);
        while (matcher.find()) {
            string = string.replace(matcher.group(0), "");
        }
        string = string.replace(" ", "");
        return string;
    }
}
